package com.cskaoyan.order.service.impl;

import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderQueryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javafx.scene.chart.BubbleChart;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description:
 * @since: 2022-10-25 21:45
 * @author: lazy
 */

@Slf4j
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Override
    public OrderListResponse orderList(OrderListRequest request) {


        return null;
    }

    @Override
    public OrderDetailResponse orderDetail(String orderId) {


        List<OrderItemDto> list1 = orderItemMapper.queryByOrderId1(orderId);
        OrderDetailResponse orderDetailResponse = orderItemMapper.queryByUserInfoByOrderId(orderId);
        orderDetailResponse.setGoodsList(list1);

        return orderDetailResponse;
    }

    @Override
    public OrderListResponse queryAllOrdersByUid(OrderListRequest pageInfo) {

        PageHelper.startPage(pageInfo.getPage(), pageInfo.getSize());
        Example example = new Example(Order.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("create_time desc");
        criteria.andEqualTo("userId", pageInfo.getUserId());
        List<Order> orders = orderMapper.selectByExample(example);



        List<OrderDetailInfo> orderDetailInfos = new ArrayList<>();
        for (Order order : orders) {
            OrderDetailInfo orderDetailInfo = new OrderDetailInfo();
            orderDetailInfo.setUserId(order.getUserId());
            orderDetailInfo.setOrderId(order.getOrderId());
            orderDetailInfo.setPayment(order.getPayment());
            orderDetailInfo.setStatus(order.getStatus());
            orderDetailInfo.setUpdateTime(order.getUpdateTime());
            orderDetailInfo.setCreateTime(order.getCreateTime());
            orderDetailInfos.add(orderDetailInfo);
        }
         // List<OrderDetailInfo> orderDetailInfo = orderMapper.queryAllOrdersByUserId(pageInfo.getUserId(), pageInfo.getSize(), pageInfo.getPage());


        // int fromIndex = (pageInfo.getPage() - 1) * pageInfo.getSize();
        // int toIndex = (pageInfo.getPage() - 1) * pageInfo.getSize() + pageInfo.getSize();

        // if (toIndex > orderDetailInfo.size()) {
        //
        //     toIndex=orderDetailInfo.size();
        // }
        // List<OrderDetailInfo> orderDetailInfos = orderDetailInfo.subList(fromIndex, toIndex);
        for (OrderDetailInfo detailInfo : orderDetailInfos) {
            List<OrderItemDto> orderItemDto = orderItemMapper.queryByOrderId1(detailInfo.getOrderId());
            detailInfo.setOrderItemDto(orderItemDto);
            OrderShippingDto orderShippingDto = orderShippingMapper.queryShippingByOrderId(detailInfo.getOrderId());
            detailInfo.setOrderShippingDto(orderShippingDto);
        }
        long count = orderMapper.countAllByUserId(pageInfo.getUserId());
        OrderListResponse orderListResponse = new OrderListResponse();
        orderListResponse.setData(orderDetailInfos);
        orderListResponse.setTotal(count);
        return orderListResponse;
    }

    @Override
    public Integer queryStatusByOrderId(String orderId) {
        Integer status = orderMapper.queryStatusByOrderId(orderId);
        return status;
    }
}
